package it.rikesh.aip2011.client.component;

import it.rikesh.aip2011.client.BaseLayout;
import it.rikesh.aip2011.client.service.TrailService;
import it.rikesh.aip2011.client.service.TrailServiceAsync;
import it.rikesh.aip2011.shared.model.FaunaFlora;
import it.rikesh.aip2011.shared.model.Hut;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Text;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Frame;
import com.google.gwt.user.client.ui.HTML;

/**
 * @author  sunita
 */
public class TrailLayout extends LayoutContainer {
    private static TrailServiceAsync trailService = GWT.create(TrailService.class);
    
    private Text txtName;
    private HTML frame;
    private Text txtDescription;
    private Text txtActivity;
    private Text lblDescription;
    private Text lblActivity;
    private Text lblHut;
    private Text lblFFauna;
    private List<Hut> huts;
    private List<FaunaFlora> ffauna;
    private FlexTable tableHut;
    private FlexTable tableFFauna;
    private String id;
    
    public TrailLayout(String name,boolean link){
        trailService.getTrailId(name, new AsyncCallback<String>() {

            public void onFailure(Throwable caught) {
                
            }

            public void onSuccess(String result) {
                id=result;   
                init();
                getInfoTrail(id);
                
                addComponent();
            }
        });
        
    }
    
    public TrailLayout(String id){
        init();
        getInfoTrail(id);
        
        addComponent();
    }
    
    /**
     * Questo metodo si occupa di inizializzare tutte le variabili necessari
     */
    public void init(){
        lblDescription=new Text("DESCRIZIONE");
        lblDescription.setStyleAttribute("color", "red");
        lblDescription.setStyleAttribute("text-align", "center");
        lblDescription.setStyleAttribute("font-weight", "bold");
        lblDescription.setStyleAttribute("font-size", "18px");
        frame=new HTML();
        lblHut=new Text("RIFUGI RAGGIUNTI");
        lblHut.setStyleAttribute("color", "red");
        lblHut.setStyleAttribute("text-align", "center");
        lblHut.setStyleAttribute("font-weight", "bold");
        lblHut.setStyleAttribute("font-size", "18px");
        lblFFauna=new Text("FLORA E FAUNA PRESENTI LUNGO IL PERCORSO");
        lblFFauna.setStyleAttribute("color", "red");
        lblFFauna.setStyleAttribute("text-align", "center");
        lblFFauna.setStyleAttribute("font-weight", "bold");
        lblFFauna.setStyleAttribute("font-size", "18px");
        lblActivity=new Text("ATTIVITA' SVOLTA SU QUESTO PERCORSO");
        lblActivity.setStyleAttribute("color", "red");
        lblActivity.setStyleAttribute("text-align", "center");
        lblActivity.setStyleAttribute("font-weight", "bold");
        lblActivity.setStyleAttribute("font-size", "18px");
        txtActivity=new Text();
        txtName=new Text();
        txtName.setStyleAttribute("color", "red");
        txtName.setStyleAttribute("text-align", "center");
        txtName.setStyleAttribute("font-weight", "bold");
        txtName.setStyleAttribute("font-size", "35px");
        txtName.setStyleAttribute("font-family", "Rock Salt");
        txtDescription=new Text();
        txtDescription.setStyleAttribute("text-align", "center");
        tableHut=new FlexTable();
        tableFFauna=new FlexTable();
    }
    
    /**
     * Permettere di ottenere tutti dati nel datatstore relativo al rifugio
     * @param id id del rifugio da visualizzare
     */
    private void getInfoTrail(String id){
        trailService.getHutTrail(id, new AsyncCallback<List<Hut>>() {
            
            public void onSuccess(List<Hut> result) {
                huts=new ArrayList<Hut>();
                for (int i=0;i<result.size();i++) {
                    huts.add(result.get(i));
                    Text h=new Text(result.get(i).getName());
                    h.setStyleAttribute("text-decoration", "underline");
                    h.setStyleAttribute("color", "blue");
                    h.addListener(Events.OnClick, new Listener<BaseEvent>() {
                        
                        public void handleEvent(BaseEvent be) {
                            BaseLayout.changeContent(new ActivityLayout(((Text)be.getSource()).getText(),true));
                        }
                    });
                    tableHut.setWidget(i, 0, h);
                }
                layout();
                
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        trailService.getTrailFFauna(id, new AsyncCallback<List<FaunaFlora>>() {
            
            public void onSuccess(List<FaunaFlora> result) {
                ffauna=new ArrayList<FaunaFlora>();
                for (int i=0;i<result.size();i++) {
                    ffauna.add(result.get(i));
                    Text ff=new Text(result.get(i).getName());
                    ff.setStyleAttribute("text-decoration", "underline");
                    ff.setStyleAttribute("color", "blue");
                    ff.addListener(Events.OnClick, new Listener<BaseEvent>() {
                        
                        public void handleEvent(BaseEvent be) {
                            BaseLayout.changeContent(new ActivityLayout(((Text)be.getSource()).getText(),true));
                        }
                    });
                    tableFFauna.setWidget(i, 0, ff);
                }
                layout();
                
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        trailService.getTrailName(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                txtName.setText(result);
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        trailService.getTrailDDescription(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                txtDescription.setText(result);
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        trailService.getTrailLink(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                frame.setHTML("<center><iframe src='"+result+"' style='width: 65%; height: 250px; '></iframe></center>");
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
        
        trailService.getTrailActivity(id, new AsyncCallback<String>() {
            
            public void onSuccess(String result) {
                txtActivity.setText(result);
                txtActivity.setStyleAttribute("text-decoration", "underline");
                txtActivity.setStyleAttribute("color", "blue");
                txtActivity.addListener(Events.OnClick, new Listener<BaseEvent>() {
                    
                    public void handleEvent(BaseEvent be) {
                        BaseLayout.changeContent(new ActivityLayout(((Text)be.getSource()).getText(),true));
                    }
                });
                layout();
            }
            
            public void onFailure(Throwable caught) {
                
            }
        });
    }
    
    private void addComponent(){
        add(txtName);
        add(frame);
        add(lblDescription);
        add(txtDescription);
        add(lblHut);
        add(tableHut);
        add(lblActivity);
        add(txtActivity);
        add(lblFFauna);
        add(tableFFauna);
    }
}
